<template>
  <div class="login-container">
    <!-- 左侧轮播图 -->
    <div class="carousel-container">
      <el-carousel height="100%" :interval="3000" arrow="always" indicator-position="outside" :autoplay="true" trigger="hover">
        <el-carousel-item v-for="item in carouselImages" :key="item.id">
          <div class="carousel-item">
            <img :src="item.url" :alt="item.title" class="carousel-image">
            <div class="carousel-overlay"></div>
            <div class="carousel-caption">
              <h3>{{ item.title }}</h3>
              <p>{{ item.description }}</p>
            </div>
          </div>
        </el-carousel-item>
      </el-carousel>
    </div>

    <!-- 右侧登录表单 -->
    <div class="form-container">
      <el-card class="login-card">
        <h2>驾校管理系统</h2>
        
        <!-- 登录表单 -->
        <el-form v-if="!isRegister" :model="loginForm" :rules="loginRules" ref="loginForm" label-width="80px" class="login-form">
          <el-form-item label="用户名" prop="username">
            <el-input v-model="loginForm.username" placeholder="请输入用户名"></el-input>
          </el-form-item>
          
          <el-form-item label="密码" prop="password">
            <el-input v-model="loginForm.password" type="password" placeholder="请输入密码"></el-input>
          </el-form-item>

          <el-form-item label="用户类型" prop="role_id">
            <el-select v-model="loginForm.role_id" placeholder="请选择用户类型" style="width: 100%">
              <el-option label="学员" :value="0"></el-option>
              <el-option label="教练" :value="1"></el-option>
              <el-option label="管理员" :value="2"></el-option>
            </el-select>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="handleLogin" style="width: 100%">登录</el-button>
          </el-form-item>
          
          <el-form-item>
            <el-button type="text" @click="switchToRegister">还没有账号？学员点此注册</el-button>
          </el-form-item>
        </el-form>

        <!-- 注册表单 -->
        <el-form v-else :model="registerForm" :rules="registerRules" ref="registerForm" label-width="100px" class="register-form">
          <el-form-item label="用户名" prop="username">
            <el-input v-model="registerForm.username" placeholder="请输入用户名"></el-input>
          </el-form-item>

          <el-form-item label="密码" prop="password">
            <el-input v-model="registerForm.password" type="password" placeholder="请输入密码"></el-input>
          </el-form-item>

          <el-form-item label="姓名" prop="name">
            <el-input v-model="registerForm.name" placeholder="请输入真实姓名"></el-input>
          </el-form-item>

          <el-form-item label="性别" prop="gender">
            <el-radio-group v-model="registerForm.gender">
              <el-radio label="male">男</el-radio>
              <el-radio label="female">女</el-radio>
            </el-radio-group>
          </el-form-item>

          <el-form-item label="身份证号" prop="id_card_number">
            <el-input v-model="registerForm.id_card_number" placeholder="请输入身份证号"></el-input>
          </el-form-item>

          <el-form-item label="手机号码" prop="phone_number">
            <el-input v-model="registerForm.phone_number" placeholder="请输入手机号码"></el-input>
          </el-form-item>

          <el-form-item label="电子邮箱" prop="email">
            <el-input v-model="registerForm.email" placeholder="请输入电子邮箱"></el-input>
          </el-form-item>

          <el-form-item label="出生日期" prop="date_of_birth">
            <el-date-picker v-model="registerForm.date_of_birth" type="date" placeholder="选择出生日期"></el-date-picker>
          </el-form-item>

          <el-form-item label="地址" prop="address">
            <el-input 
              v-model="registerForm.address" 
              type="textarea" 
              placeholder="请输入详细地址">
            </el-input>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="handleRegister" style="width: 100%">注册</el-button>
          </el-form-item>

          <el-form-item>
            <el-button type="text" @click="switchToLogin">已有账号？返回登录</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script>

export default {
  name: 'LoginDemo',
  data() {
    // 自定义验证规则
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码长度不能小于6位'))
      } else {
        callback()
      }
    }

    const validateIdCard = (rule, value, callback) => {
      const reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/
      if (!reg.test(value)) {
        callback(new Error('请输入正确的身份证号码'))
      } else {
        callback()
      }
    }

    const validatePhone = (rule, value, callback) => {
      const reg = /^1[3-9]\d{9}$/
      if (!reg.test(value)) {
        callback(new Error('请输入正确的手机号码'))
      } else {
        callback()
      }
    }

    return {
      isRegister: false,
      loginForm: {
        username: '',
        password: '',
        role_id: ''
      },
      registerForm: {
        username: '',
        password: '',
        role_id: 0, // 默认为学员
        name: '',
        gender: 'male',
        id_card_number: '',
        phone_number: '',
        email: '',
        address: '',
        date_of_birth: ''
      },
      loginRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { validator: validatePassword, trigger: 'blur' }
        ],
        role_id: [
          { required: true, message: '请选择用户类型', trigger: 'change' }
        ]
      },
      registerRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { validator: validatePassword, trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ],
        gender: [
          { required: true, message: '请选择性别', trigger: 'change' }
        ],
        id_card_number: [
          { required: true, message: '请输入身份证号码', trigger: 'blur' },
          { validator: validateIdCard, trigger: 'blur' }
        ],
        phone_number: [
          { required: true, message: '请输入手机号码', trigger: 'blur' },
          { validator: validatePhone, trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱地址', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
        ],
        date_of_birth: [
          { required: true, message: '请选择出生日期', trigger: 'change' }
        ],
        address: [
          { required: true, message: '请输入地址', trigger: 'blur' }
        ]
      },
      carouselImages: [
        {
          id: 1,
          url: require('@/assets/img/pic1.png'),
          title: '一流教练团队',
          description: '经验丰富，耐心指导'
        },
        {
          id: 2,
          url: require('@/assets/img/pic2.png'),
          title: '专业驾驶培训',
          description: '规范教学，安全至上'
        },
        {
          id: 3,
          url: require('@/assets/img/pic3.png'),
          title: '专业驾驶培训',
          description: '灵活排课，贴心服务'
        }
      ],
    }
  },
  methods: {
    // 切换到注册界面
    switchToRegister() {
      this.isRegister = true
      this.$nextTick(() => {
        this.$refs.registerForm?.resetFields()
      })
    },
    // 切换到登录界面
    switchToLogin() {
      this.isRegister = false
      this.$nextTick(() => {
        this.$refs.loginForm?.resetFields()
      })
    },
    // 处理登录
    handleLogin() {
      this.$refs.loginForm.validate(async (valid) => {
        if (valid) {
          try {
            // 这里添加登录接口调用
            await this.$axios.post('/api/login', this.loginForm)
            this.$message.success('登录成功')
            // 根据角色跳转到不同的页面
            const roleRoutes = {
              0: '/userDemo',
              1: '/coachDemo',
              2: '/adminDemo'
            }
            this.$router.push(roleRoutes[this.loginForm.role_id])
          } catch (error) {
            this.$message.error('登录失败，请重试')
          }
        }
      })
    },
    // 处理注册
    handleRegister() {
      this.$refs.registerForm.validate(async (valid) => {
        if (valid) {
          try {
            // 这里添加注册接口调用
            await this.$axios.post('/api/register', this.registerForm)
            this.$message.success('注册成功')
            // 注册成功后切换到登录界面
            this.switchToLogin()
          } catch (error) {
            this.$message.error('注册失败，请重试')
          }
        }
      })
    },
  }
}
</script>

<style scoped>
.login-container {
  height: 100vh;
  display: flex;
  background-color: #f5f7fa;
}

.carousel-container {
  flex: 1;
  height: 100vh;
  overflow: hidden;
  position: relative;
}

.carousel-item {
  height: 100%;
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
}

.carousel-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.1s ease;
}

.carousel-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.2); /* 降低遮罩层透明度 */
}

.carousel-caption {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  text-align: center;
  color: #fff;
  z-index: 2;
  width: 80%;
  max-width: 600px;
}

.carousel-caption h3 {
  font-size: 32px;
  margin-bottom: 20px;
  font-weight: bold;
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
}

.carousel-caption p {
  font-size: 18px;
  margin: 0;
  line-height: 1.5;
  text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.5);
}

.form-container {
  width: 480px;
  display: flex;
  align-items: center;
  padding: 40px;
  background: linear-gradient(135deg, #ffffff 0%, #f8f9fa 100%);
  position: relative;
  box-shadow: -5px 0 15px rgba(0, 0, 0, 0.1);
}

.login-card {
  width: 100%;
  padding: 30px;
  background: #ffffff;
  border-radius: 15px;
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.05);
}

.login-card h2 {
  text-align: center;
  margin-bottom: 25px;
  color: #2c3e50;
  font-size: 28px;
  font-weight: 600;
  position: relative;
}

.login-card h2::after {
  content: '';
  position: absolute;
  bottom: -10px;
  left: 50%;
  transform: translateX(-50%);
  width: 50px;
  height: 3px;
  background: linear-gradient(90deg, #409EFF, #53a8ff);
  border-radius: 3px;
}

/* 表单项样式 */
.el-form-item {
  margin-bottom: 15px;
}

/* 注册表单特殊样式 */
.el-form:not(.login-form) .el-form-item {
  margin-bottom: 12px;
}

/* 最后一个表单项保持一定间距 */
.el-form-item:last-child {
  margin-bottom: 20px;
}

/* 调整按钮上方间距 */
.el-form-item:has(.el-button) {
  margin-top: 20px;
}

.el-input__inner {
  height: 45px;
  line-height: 45px;
  border-radius: 8px;
  border: 1px solid #dcdfe6;
  transition: all 0.3s;
}

.el-input__inner:hover {
  border-color: #409EFF;
}

.el-input__inner:focus {
  border-color: #409EFF;
  box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.1);
}

/* 按钮样式 */
.el-button--primary {
  height: 45px;
  font-size: 16px;
  font-weight: 500;
  border-radius: 8px;
  background: linear-gradient(135deg, #409EFF 0%, #53a8ff 100%);
  border: none;
  transition: all 0.3s;
}

.el-button--primary:hover {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(64, 158, 255, 0.3);
}

.el-button--text {
  margin: 15px auto 0;
  display: block;
  font-size: 14px;
  color: #409EFF;
  transition: all 0.3s;
}

.el-button--text:hover {
  color: #53a8ff;
  transform: translateY(-1px);
}

/* 下拉选择框样式 */
.el-select .el-input__inner {
  cursor: pointer;
}

/* 单选框组样式 */
.el-radio {
  margin-right: 20px;
}

.el-radio__input.is-checked .el-radio__inner {
  background: #409EFF;
  border-color: #409EFF;
}

/* 日期选择器样式 */
.el-date-picker {
  width: 100%;
  margin-bottom: 0;
}

.el-date-picker__header {
  margin: 8px;
}

/* 文本域样式 */
.el-textarea__inner {
  border-radius: 8px;
  min-height: 60px;
  transition: all 0.3s;
}

.el-textarea__inner:hover {
  border-color: #409EFF;
}

.el-textarea__inner:focus {
  border-color: #409EFF;
  box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.1);
}

/* 表单标签样式 */
.el-form-item__label {
  font-weight: 500;
  color: #2c3e50;
}

/* 添加响应式适配 */
@media screen and (max-width: 768px) {
  .form-container {
    width: 100%;
    padding: 20px;
  }
  
  .login-card {
    padding: 20px;
  }
  
  .login-card h2 {
    font-size: 24px;
  }
}

/* 轮播图箭头和指示器样式 */
.el-carousel__arrow {
  background-color: rgba(0, 0, 0, 0.3);
  border: none;
  width: 36px;
  height: 36px;
}

.el-carousel__arrow:hover {
  background-color: rgba(0, 0, 0, 0.5);
}

.el-carousel__indicators {
  z-index: 2;
}

/* 增强轮播图过渡效果 */
.el-carousel__item {
  transition: all 0.3s ease;
}

.el-carousel__item:hover .carousel-image {
  transform: scale(1.05);
}

/* 优化指示器样式 */
.el-carousel__indicator--horizontal {
  padding: 12px 4px;
}

.el-carousel__button {
  width: 30px;
  height: 3px;
  background-color: rgba(255, 255, 255, 0.7);
  border-radius: 3px;
}

.el-carousel__indicator.is-active .el-carousel__button {
  background-color: #fff;
}

/* 保持原有其他样式不变 */
h2 {
  text-align: center;
  margin-bottom: 30px;
  color: #303133;
}

.el-button--text {
  margin: 0 auto;
  display: block;
}

.el-date-picker {
  width: 100%;
}

/* 修改轮播图组件样式 */
.el-carousel, .el-carousel__container {
  height: 100% !important;
}

.el-carousel__item {
  height: 100%;
}

/* 确保图片容器占满轮播项 */
.el-carousel__item, .carousel-item {
  height: 100%;
  width: 100%;
}
</style>